package com.guodi.map.controller;

import com.guodi.common.constant.StatusCode;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapSceneConfig;
import com.guodi.map.service.IMapSceneConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author pwm
 */
@Validated
@RestController
@RequestMapping("/MapSceneConfig")
@Api(value = "API - MapSceneConfigController", tags = "三维地图场景模块配置")
public class MapSceneConfigController extends BaseController {

    @Autowired
    private IMapSceneConfigService mapSceneConfigService;

    /**
     * 根据主键查询
     *
     * @param id id
     * @return R
     */
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R<?> findById(String id) {
        if (StringUtils.isBlank(id)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "id不能为空");
        }
        MapSceneConfig mapSceneConfig = mapSceneConfigService.getById(id);
        return R.data(mapSceneConfig);
    }


    @PostMapping("/getSceneConfig")
    @ApiOperation(value = " 返回场景配置(一张图)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneCode", value = "场景编码", required = true, dataType = "string", paramType = "query")
    })
    public R<?> getSceneConfig(String sceneCode) {
        if (StringUtils.isBlank(sceneCode)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "场景编码不能为空");
        }
        MapSceneConfig mapSceneConfig = mapSceneConfigService.selectBySceneCode(sceneCode);
        return R.data(mapSceneConfig);
    }

    @PostMapping("/getSceneConfigBySceneId")
    @ApiOperation(value = " 返回场景配置（配置中心）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneId", value = "场景id", required = true, dataType = "string", paramType = "query")
    })
    public R<?> getSceneConfigBySceneId(String sceneId) {
        if (StringUtils.isBlank(sceneId)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "场景id不能为空");
        }
        MapSceneConfig mapSceneConfig = mapSceneConfigService.selectBySceneId(sceneId);
        return R.data(mapSceneConfig);
    }

    /**
     * 修改
     *
     * @param id id
     * @param scene scene
     * @return R
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "scene", value = "场景配置json", required = true,
                    dataType = "string", paramType = "query")
    })
    public R<?> update(String id, String scene) {
        if (StringUtils.isBlank(id)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "场景配置id不能为空");
        }
        if (StringUtils.isBlank(scene)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "场景配置信息不能为空");
        }
        MapSceneConfig byId = mapSceneConfigService.getById(id);
        if (byId == null) {
            return R.fail(StatusCode.ERROR_CODE_40005, "没有对应的场景配置信息");
        }
        mapSceneConfigService.prepareData(byId, false);
        byId.setScene(scene);
        mapSceneConfigService.updateById(byId);
        return R.data(byId);
    }

}
